__usage__='''Call examples:
1)python run_formtest.py -quite -problem=problemName ... output includes progressbar and colors.
2)python run_formtest.py -problem=problemName ... output has no progressbar and no colors. Suitable for redi-
recting to file.

Argument problemName is either 'ds' for driven square, 'rs' for rotated square or 'zd' for Zalesak's disk or 
'all' for all problems.'''

from problems.aux import get_problem

from solvers.formtest import backwardeuler
from solvers.formtest.forms import allForms

from graphics.lineplot import LinePlot
from graphics.colorprint import ColorPrint,colors
import time,sys,random

if __name__ == '__main__':
    if not(len(sys.argv) == 2 or len(sys.argv) == 3):
        print __usage__
        sys.exit()
    elif len(sys.argv) == 2:
        if not('-problem=' in sys.argv[1][:9]):
            print __usage__
            sys.exit()
        else:
            sColor = 'red'
            iColor = random.choice(colors.keys())
            pgb = True
            problemName = sys.argv[1][9:]
    else: 
        if not(sys.argv[1] == '-quite' and '-problem=' in sys.argv[2][:9]):
            print __usage__
            sys.exit()
        else:
            sColor = 'normal'
            iColor = 'normal'
            pgb = False
            problemName = sys.argv[2][9:]

    # hardcoded
    options = {'N' : 50}                            # spatial resolution
    options['p'] = 1                                # polynomial degree
    options['results-path'] = '../result-form-tests-stab' # path to results 

    lp = LinePlot()
    cp = ColorPrint(colors)

    for F in allForms:
        for problem, sf in get_problem(problemName,options):
            solver = backwardeuler.BackwardEuler(p=options['p'],F=F)
            solver.pgb = pgb
            solver.sf = sf

            print '%s' % ('-'*70)
       
            print cp('Solving %s problem using %s %s method:' % (problem.problemDir,solver.methodDir,solver.solverDir),iColor)
            start = time.time()
            solver.solve(problem)
            lp.plot_all(problem,solver)
            print cp('\nDone(CFL=%g) in %s%s' % (solver.CFL,cp('%g s' % (time.time()-start),sColor),cp('!',iColor)),iColor)
        
            print '%s' % ('-'*70)
            del solver, problem


